package xixirupan.tmgp.tencent.com.vmeim.ui.beautycircle.presenter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;
import java.util.Map;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import xixirupan.tmgp.tencent.com.vmeim.R;
import xixirupan.tmgp.tencent.com.vmeim.config.URLConfig;
import xixirupan.tmgp.tencent.com.vmeim.ui.beautycircle.model.PostsBaen;
import xixirupan.tmgp.tencent.com.vmeim.ui.beautycircle.model.TagsBaen;

/**
 * Created by lenovo on 2016/10/20.
 */

public class EssenseRecyAdapter   extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private static final int TYPE2 = 2;
    private static final int TYPE1 = 1;
    private List<Object> lists;
    private Context context;
    private LayoutInflater inflater;
    public EssenseRecyAdapter(List<Object> lists,Context context,LayoutInflater inflater)
    {
        this.context =context;
        this.inflater =inflater;
        this.lists =lists;
    }

    @Override
    public int getItemViewType(int position) {
        //如果是集合类型的数据源
        if (lists.get(position) instanceof List)
        {
            Log.i("集合类型","--------->"+position);
            return TYPE1;
        }
        //如果是Postbean类型的数据源
       else  if (lists.get(position) instanceof PostsBaen)
        {
            Log.i("postbean","--------->"+position);

            return TYPE2;
        }
        return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         View view;
       if (viewType ==TYPE1)
       {
           view =inflater.inflate( R.layout.item1_essense_recycler,parent,false);
          ViewHolderE1 e1 =new ViewHolderE1(view);
           return e1;
       }else  if (viewType ==TYPE2)
       {
           View inflate = inflater.inflate(R.layout.item2_essense_recycler, parent, false);
           ViewHolderE2 e2 =new ViewHolderE2(inflate);
           return e2;
       }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

            if (holder instanceof ViewHolderE1){
                 if(position<10) {
                     List<Map<String, String>> lists_map = (List<Map<String, String>>) lists.get(position);

                     String headUrl = lists_map.get(position).get("headUrl");
                     String nickname = lists_map.get(position).get("nickname");
                     String typeTitle = lists_map.get(position).get("typeTitle");
                     String userGradeName = lists_map.get(position).get("userGradeName");
                     ((ViewHolderE1) holder).imageview.setImageURI(headUrl);
                     ((ViewHolderE1) holder).textView_nickname.setText(nickname);
                     ((ViewHolderE1) holder).textView_typetitle.setText(typeTitle);
                     ((ViewHolderE1) holder).textView_usergrade.setText(userGradeName);
                     //点击事件添加好友 判断登陆情况
                     ((ViewHolderE1) holder).imageView_add.setOnClickListener(new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {

                         }
                     });

                 }  }else  if (holder instanceof ViewHolderE2)
            {

                  PostsBaen postsBaen = (PostsBaen) lists.get(position);
                //内容
                  final String content = postsBaen.getContent();
                int collectNum = postsBaen.getCollectNum();
                //图片
                String cover = postsBaen.getCover();
                //标签流式布局
                List<TagsBaen> tags = postsBaen.getTags();
                int like = postsBaen.getLike();
                int collectNum1 = postsBaen.getCollectNum();
                ((ViewHolderE2) holder).textView_content.setText(content);
                Glide.with(context).load(URLConfig.Url.ESSENSE_ICON+cover).into(((ViewHolderE2) holder).imageView);
                    ((ViewHolderE2) holder).textView_collectnum.setText(collectNum+"");
                ((ViewHolderE2) holder).textView_likenum.setText(like+"");
                //分享按钮的点击事件
                ((ViewHolderE2) holder).imgaview_share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       // Toast.makeText(context,"分享事件",Toast.LENGTH_SHORT).show();
                        showShare();

                    }
                });
            }


    }

    @Override
    public int getItemCount() {
        Log.i("精华部分adapter中数据源个数","+========="+lists.size());
        return lists.size();
    }


    class ViewHolderE1 extends RecyclerView.ViewHolder

    {
             SimpleDraweeView imageview;
         TextView textView_nickname;
        TextView textView_typetitle;
        TextView textView_usergrade;
        ImageView imageView_add;
        public ViewHolderE1(View itemView) {
            super(itemView);
             imageview = (SimpleDraweeView) itemView.findViewById(R.id.itme1_essense_imgIcon);
              textView_nickname = (TextView) itemView.findViewById(R.id.item1_esses_nickname);
              textView_typetitle = (TextView) itemView.findViewById(R.id.item1_esses_typetitle);
              textView_usergrade  = (TextView) itemView.findViewById(R.id.item1_esses_typetitle);
              imageView_add = (ImageView) itemView.findViewById(R.id.item1_esses_iconadd);
        }
    }
    class ViewHolderE2 extends RecyclerView.ViewHolder
    {
       ImageView imageView;
        TextView textView_content;
        RecyclerView item2_recycler;
        TextView textView_likenum;
        TextView textView_collectnum;
        ImageView imgaview_share;

        public ViewHolderE2(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.item2_essense_img);
            textView_content = (TextView) itemView.findViewById(R.id.item2_essense_content);
            item2_recycler = (RecyclerView) itemView.findViewById(R.id.item2_essense_recyclertag);
            textView_likenum = (TextView) itemView.findViewById(R.id.item2_essense_likenum);
            textView_collectnum = (TextView) itemView.findViewById(R.id.item2_essense_collectnum);
            imgaview_share = (ImageView) itemView.findViewById(R.id.item2_essense_share);

        }
    }
    public void reloadListView(List<Object> _list, boolean isClear) {
        if (isClear) {
            lists.clear();
        }
        lists.addAll(_list);
        notifyDataSetChanged();
    }

    private void showShare() {
        ShareSDK.initSDK(context);
        OnekeyShare oks = new OnekeyShare();
//关闭sso授权
        oks.disableSSOWhenAuthorize();

// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle("标题");
// titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl("http://weibo.com/3021421033/profile?topnav=1&wvr=6&is_all=1");
// text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
// imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
// url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://weibo.com/3021421033/profile?topnav=1&wvr=6&is_all=1");
// comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
// site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(context.getString(R.string.app_name));
// siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

 // 启动分享GUI
        oks.show(context);
    }

}
